package com.bai.order.service.impl;

import com.bai.order.client.EduClient;
import com.bai.order.client.UcenterClient;
import com.bai.order.entity.Order;
import com.bai.order.mapper.OrderMapper;
import com.bai.order.service.OrderService;
import com.bai.utils.OrderNoUtil;
import com.bai.utils.orderVo.CourseOrderVo;
import com.bai.utils.orderVo.UcenterMemberOrder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author bai
 * @since 2023-08-19
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;

    /**
     *  根据课程id和用户id创建订单
     * @param courseId
     * @param id
     * @return
     */
    @Override
    public String createOrders(String courseId, String id) {
        //远程调用根据用户id获取用户信息
        UcenterMemberOrder memberOrder = ucenterClient.getById(id);
        //远程调用根据课程id获取课程信息
        CourseOrderVo courseInfoOrder = eduClient.getCourseInfoOrder(courseId);
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());//订单号
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(id);
        order.setMobile(memberOrder.getMobile());
        order.setNickname(memberOrder.getNickname());
        order.setStatus(0);//未支付
        order.setPayType(1);//1：微信
        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
